package com.briup.cms.service;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.Category;
import com.briup.cms.dao.ArticleMapper;
import com.briup.cms.dao.CategoryMapper;
import com.briup.cms.exception.CategoryException;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Service
public class CategoryServiceImpl implements ICategoryService{

    @Autowired
    private CategoryMapper mapper;

    @Autowired
    private ArticleMapper mapper2;

    @Override
    public PageInfo<Category> pageQueryAll(int pageNum, int pageSize) throws CustomerException {
        PageHelper.startPage(pageNum, pageSize,true);
        List<Category> list = mapper.findAll();
        PageInfo<Category> info = new PageInfo<>(list);
        return info;
    }

    @Override
    @Transactional
    public void  deleteById(int id) throws CategoryException{
        if(mapper.findById(id)==null){
            throw new CategoryException(MessageStatus.SPECIFIED_QUESTIONED_CATEGORY_NOT_EXIST);
        }
        try {
            List<Category> list = mapper.findByParentId(id);
//            List<Article> list2 = mapper2.findByIdWithCategory(id);
            if(list != null){
                for(Category category:list){
                    category.setCategoryParentId(null);
                    mapper.updateCategory(category);
                }
            }
//            if(list2 != null){
//                for(Article article:list2){
//                    mapper2.updateCategoryId(article);
//                }
//            }
            mapper.deleteById(id);
        } catch (Exception e) {
            throw new CategoryException(MessageStatus.SPECIFIED_QUESTIONED_BATCHDELETE_ARTICLE_ERROR);
        }
    }

    @Override
    @Transactional
    public void batchDelete(int[] ids) throws CategoryException{
        Vector<Integer> v_ids = new Vector();
        for(int id:ids){
            int max_index=-1;
            int j=0;
            // 为防止 已经被删除的栏目也操作成功
            // 如果栏目已经不存在，抛出数据不存在的异常
            if(mapper.findById(id)==null){
                throw new CategoryException(MessageStatus.SPECIFIED_QUESTIONED_CATEGORY_NOT_EXIST);
            }
            try {
                //判断是不是父栏目（有没有子节点） 并返回找到的所有子节点
                List<Category> list = mapper.findByParentId(id);
                if(!list.isEmpty()){
                    //找最后的子节点
                    for (Category category : list){
                        for (int i = 0 ; i < v_ids.size() ; i++){
                            System.out.println("categoryid:..."+category.getCategoryId());
                            if (category.getCategoryId().equals(v_ids.get(i))){
                                if (max_index < i) max_index = i;
                            }
                        }

                    }
//                    System.out.println("max_index"+ max_index);
                    if(max_index == -1){  //如果没找到，但是有子节点，就放在最后
                        v_ids.add(id);
                    }
                    else{   //如果找到了就在子节点的后面进行插入
                        v_ids.add(max_index+1,id);
                    }

                }
                else{
                    // 没有子节点 放在最前面
                    v_ids.add(0,id);
                }
            } catch (Exception e) {
                throw new CategoryException(MessageStatus.SPECIFIED_QUESTIONED_BATCHDELETE_ERROR);
            }
        }
        for(int id:v_ids){
            System.out.println(id);
        }
        // 对数组依次删除 int i=0;i<v_ids.size();i++
//        for(int id:v_ids){
//            int id = Integer.parseInt(v_ids.get(i).toString());
//            mapper.deleteById(id);
//        }

    }

    @Override
    public void saveCategory(Category category) throws CustomerException {
            mapper.saveCategory(category);
    }

    @Override
    public void updateCategory(Category category) throws CustomerException {
            mapper.updateCategory(category);
    }

    @Override
    public List<Category> findAll() throws CustomerException {
        return mapper.findAll();
    }
}
